<?php
/**
 * *********************************************************************************************************************
 * 获取Alipay 用户的相关信息
 * *********************************************************************************************************************
 */
namespace soa\message;
use soa\message\alipayServiceWindow\serviceWindow;

class userInfoAlipay extends serviceWindow
{
    /**
     * 获取auth token 接口地址
     * @var string
     */
    private $alipayAuthMethod = 'alipay.system.oauth.token';

    /**
     * 获取用户信息接口
     * @var string
     */
    private $alipayUserMethod = 'alipay.user.userinfo.share';
    /**
     * appId
     * @var string
     */
    private $appId = '2015051800078634';

    public function getAlipayAuthMethod()
    {
        return $this->alipayAuthMethod;
    }

    public function getAlipayUserMethod()
    {
        return $this->alipayUserMethod;
    }

    public function getAppId()
    {
        return $this->appId;
    }

    /**
     * 通过auth_code 获取用户的userId和access_token
     * @param $auth_code
     * @return bool|array
     */
    public function getTokenAndUserId($auth_code)
    {
        if(empty($auth_code))
            return false;

        $result = $this->send($this->getAppId(), $this->getAlipayAuthMethod(), array(
            'grant_type' => 'authorization_code',
            'code' => $auth_code,
        ));
        if(!empty($result) && is_array($result))
            return array(
                'access_token' => $result['access_token'],
                'user_id' => $result['user_id'],
                'expires_in' => $result['expires_in']
            );
        elseif(is_string($result))
            return $result;
        else
            return false;
    }

    /**
     * Alipay 用户信息
     * @param $auth_token
     * @return array|bool|string
     */
    public final function getUserInfo($auth_token)
    {
        if(empty($auth_token))
            return false;

        $result = $this->send($this->getAppId(), $this->getAlipayUserMethod(), array(
            'auth_token' => $auth_token
        ));

        if(!empty($result) && is_array($result))
            return $result;
        elseif(is_string($result))
            return $result;
        else
            return array();
    }
}